import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
import requests
import concurrent.futures
import binance_klines.download.binance_history as bh


def download_and_save(
        symbol, asset_type, data_type, start, end, tz="UTC", timeframe=None):
    try:
        directory = f"binance_klines/{timeframe}"
        if not os.path.exists(directory):
            os.makedirs(directory)

        data = bh.fetch_data(
            symbol, asset_type, data_type, start, end, tz=tz, timeframe=timeframe)
        data = data.rename_axis("datetime")
        data.to_parquet(f"{directory}/{symbol}.parquet")
        print(f"save {symbol}")
    except bh.exceptions.DataNotFound as e:
        print(f"{symbol} has no data, skip: {e}")
        return
    

def get_spots_klines(start_time, end_time, timeframe, quote_coin="USDT"):
    data = requests.get("https://api.binance.com/api/v3/exchangeInfo").json()
    symbols = [i["symbol"] for i in data["symbols"] if i["status"] == "TRADING" and i["quoteAsset"] == quote_coin]
    print(f"symbols length is {len(symbols)}")

    with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
        for symbol in symbols:
            executor.submit(download_and_save, 
                *(symbol, "spot", "klines", start_time, end_time,
                  "UTC", timeframe))


if __name__ == "__main__":
    get_spots_klines("2021-01-01 00:00", "2024-04-09 23:59", "1m")
